package com.dg.lecheng.base.core.web;

import com.google.common.base.Strings;

/**
 * Created by limiemie on 2014/11/7.
 */
public class WebUtils {

    public static String getRequestAppName(String userAgent){
        //weirwood/1.1.4 (iPhone; iOS 8.0.2; Scale/2.00)
        if(Strings.isNullOrEmpty(userAgent)){
            return "unknown";
        }
        String app = userAgent.substring(0,userAgent.indexOf("(")).trim();
        String appName = app.substring(0, app.indexOf("/"));

        return appName;
    }

    public static String getRequestAppVersion(String userAgent){
        if(Strings.isNullOrEmpty(userAgent)){
            return "unknown";
        }
        String app = userAgent.substring(0,userAgent.indexOf("(")).trim();
        String appVersion = app.substring(app.indexOf("/")+1);

        return appVersion;
    }

    public static String getRequestOsName(String userAgent){
        if(Strings.isNullOrEmpty(userAgent)){
            return "unknown";
        }
        String osStr = userAgent.substring(userAgent.indexOf("(")+1,userAgent.length()-1).trim();
        if(osStr.indexOf("iOS") != -1){
            return "iOS";
        }else if(osStr.indexOf("Android") != -1){
            return "Android";
        }
        return "unknown";
    }

    public static String getRequestOsVersion(String userAgent){
        if(Strings.isNullOrEmpty(userAgent)){
            return "unknown";
        }
        String osStr = userAgent.substring(userAgent.indexOf("(")+1,userAgent.length()-1).trim();

        int start = 0;
        if(osStr.indexOf("iOS") != -1){
            start = osStr.indexOf("iOS")+3;
        }else if(osStr.indexOf("Android") != -1){
            start = osStr.indexOf("Android")+7;
        }else{
            return "unknown";
        }
        String os = osStr.substring(start).trim();
        os = os.substring(0,os.indexOf(";"));

        return os;
    }
}
